package cn.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URI;
import java.util.Properties;

import org.apache.log4j.Logger;

public class PropertyManager {
	 Logger logger1= Logger.getLogger(PropertyManager.class);
	 private Properties properties = new Properties();
	    File file;
	    /** Creates a new instance of PropertyManager */
	    public PropertyManager(String fileName) {
	        String path = getClass().getResource("/").getPath();

	        try{
	            try{
	            	path = new URI(path).getPath();
	            	//path = java.net.URLDecoder.decode(path,"utf-8");
	            	logger1.info("PropertyManager path:"+path);
	                file = new File(path,fileName);
	            }catch(Exception e){
	                file = new File(path,"/"+fileName);
	            }
	            FileInputStream fis = new FileInputStream(file);
	           
	            properties.load(fis);
	            fis.close();
	        }catch(Exception e){
	            e.printStackTrace();
	            logger1.info("PropertyManager error:"+e.getMessage());
	        }
	    }

	    public String get(String key){
	        return properties.getProperty(key.trim());
	    }

	    public String get(String key,String defaultValue){
	        return properties.getProperty(key.trim(), defaultValue);
	    }

	    public void set(String key,String value){
	        try{
	            properties.setProperty(key,value);//new String(value.getBytes("UTF-8"), "UTF-8")
	        }catch(Exception e){
	            properties.setProperty(key, value);
	            e.printStackTrace();
	        }
	    }
	    
	    public Properties getProperties(){
	    	return properties;
	    }

	    synchronized public boolean store(){
	        String path = getClass().getResource("/").getPath();
	        boolean b = false;
	        try{
	            FileOutputStream fos = new FileOutputStream(file);
	            properties.store(fos,"");
	            fos.close();
	            b = true;
	        }catch(Exception e){
	            b = false;
	            e.printStackTrace();
	        }
	        file = null;
	        return b;
	    }
}
